{
 "metadata": {
  "name": "",
  "signature": "sha256:806f7b6e2812a7f1e8ff67e708d852d06c6ed452860f8c9bb02bb4ff9ea5fdbf"
 },
 "nbformat": 3,
 "nbformat_minor": 0,
 "worksheets": [
  {
   "cells": [
    {
     "cell_type": "markdown",
     "metadata": {},
     "source": [
      "# Diagonalization of 3x3 symmetric matrix\n",
      "\n",
      "The `MassMatrix3` class uses an algorithm described in [A Method for Fast Diagonalization of a 2x2 or 3x3 Real Symmetric Matrix](http://arxiv.org/abs/1306.6291v4) by M.J. Kronenburg. This document proves that when a matrix $\\textbf{A}$ has a repeated eigenvalue and the vector $\\textbf{f}_2$ has length 0, then $\\textbf{A}$ is a diagonal matrix.\n",
      "\n",
      "The symmetric 3x3 matrix $\\textbf{A}$ has diagonal elements $A_{11}$, $A_{22}$, and $A_{33}$ and off-diagonal terms $A_{12}$, $A_{13}$, and $A_{23}$. The vector $\\textbf{f}_2$ is defined in (5.6) as\n",
      "\n",
      "$\\textbf{f}_2 = [A_{22} - A_{33}, -2A_{23}]^T$\n",
      "\n",
      "When $\\textbf{f}_2$ has length zero, the following are true:\n",
      "\n",
      "$A_{22} = A_{33} = A$\n",
      "\n",
      "$A_{23} = 0$\n",
      "\n",
      "In Section 4, conditions are given for which a matrix will have at least 1 repeated eigenvalue $\\lambda$ based on the coefficients of its characteristic equation:\n",
      "\n",
      "$\\lambda^3 - b \\lambda^2 + c \\lambda + d = 0$\n",
      "\n",
      "With $p = b^2 - 3c$ and $q = 2b^3 - 9bc - 27d$, there is at least one repeated eigenvalue when $q^2 = 4p^3$\n",
      "\n",
      "These conditions are manipulated symbolically below:"
     ]
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [
      "from sympy import *"
     ],
     "language": "python",
     "metadata": {},
     "outputs": []
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [
      "# Identify condition in which there is a repeated eigenvalue (Q^2 - 4P^3 = 0)\n",
      "B, C, D, P, Q = symbols('B C D P Q')\n",
      "P = B**2 - 3*C\n",
      "Q = 2*B**3 - 9*B*C - 27*D\n",
      "simplify(expand(Q**2 - 4*P**3))"
     ],
     "language": "python",
     "metadata": {},
     "outputs": [
      {
       "metadata": {},
       "output_type": "pyout",
       "prompt_number": 13,
       "text": [
        "-108*B**3*D - 27*B**2*C**2 + 486*B*C*D + 108*C**3 + 729*D**2"
       ]
      }
     ],
     "prompt_number": 13
    },
    {
     "cell_type": "markdown",
     "metadata": {},
     "source": [
      "First the condition in which there is a repeated eigen value ($q^2 = 4p^3$) is expressed in terms of $b$, $c$, and $d$ and simplified to:\n",
      "\n",
      "$-108b^3d - 27b^2c^2 + 486bcd + 108c^3 + 729d^2 = 0$"
     ]
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [
      "# Expand expression for which there is a repeated eigenvalue\n",
      "A11, A12, A13, A = symbols('A11 A12 A13 A')\n",
      "b, c, d = symbols('b c d')\n",
      "b = A11 + 2*A\n",
      "c = A11*A + A11*A + A**2 - A12**2 - A13**2\n",
      "d = A*A13**2 + A*A12**2 - A11*A**2\n",
      "factor(simplify(-108*b**3*d - 27*b**2*c**2 + 486*b*c*d + 108*c**3 + 729*d**2))"
     ],
     "language": "python",
     "metadata": {},
     "outputs": [
      {
       "metadata": {},
       "output_type": "pyout",
       "prompt_number": 16,
       "text": [
        "-27*(A12**2 + A13**2)**2*(A**2 - 2*A*A11 + A11**2 + 4*A12**2 + 4*A13**2)"
       ]
      }
     ],
     "prompt_number": 16
    },
    {
     "cell_type": "markdown",
     "metadata": {},
     "source": [
      "This condition is then expressed in terms of $A11$, $A$, $A12$, and $A13$ corresponding to $\\textbf{f}_2 = 0$.\n",
      "After simplification and factoring, there are 2 cases for which the expression is equal to zero:\n",
      "\n",
      "$A_{12}^2 + A_{13}^2 = 0$\n",
      "\n",
      "$4A_{12}^2 + 4A_{13}^2 + (A - A_{11})^2 = 0$\n",
      "\n",
      "In both cases, $A_{12} = A_{13} = 0$ is a pre-requisite. Since $A_{23} = 0$ was already assumed, this implies that\n",
      "the matrix must be diagonal if it has any repeated eigenvalues and $\\textbf{f}_2 = 0$."
     ]
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [],
     "language": "python",
     "metadata": {},
     "outputs": []
    }
   ],
   "metadata": {}
  }
 ]
}